//
//  LYPictureBrowseInteractiveAnimatedTransition.m
//  LYCustomTransitionDemo
//
//  Created by liyang on 2017/12/5.
//  Copyright © 2017年 liyang. All rights reserved.
//

#import "LYPictureBrowseInteractiveAnimatedTransition.h"
#import "LYPictureBrowsePushAnimator.h"
#import "LYPictureBrowsePopAnimator.h"
#import "LYPictureBrowseViewController.h"
#import "UINavigationController+FDFullscreenPopGesture.h"
#import "QABaseNavigationController.h"
@interface LYPictureBrowseInteractiveAnimatedTransition ()

@property (nonatomic, strong) LYPictureBrowsePushAnimator *customPush;
@property (nonatomic, strong) LYPictureBrowsePopAnimator  *customPop;

@end

@implementation LYPictureBrowseInteractiveAnimatedTransition

//- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
//    return self.customPush;
//}
//
//- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
//    return self.customPop;
//}
//
//- (nullable id <UIViewControllerInteractiveTransitioning>)interactionControllerForPresentation:(id <UIViewControllerAnimatedTransitioning>)animator{
//
//    return nil;//push时不加手势交互
//}
//- (nullable id <UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id <UIViewControllerAnimatedTransitioning>)animator{
//    if (self.transitionParameter.gestureRecognizer)
//        return self.percentIntractive;
//    else
//        return nil;
//
//}

//- (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
//                                            animationControllerForOperation:(UINavigationControllerOperation)operation
//                                                         fromViewController:(UIViewController *)fromVC
//                                                           toViewController:(UIViewController *)toVC{
//    if (operation == UINavigationControllerOperationPush) {
//        QABaseNavigationController *baseNavi = (QABaseNavigationController *)navigationController;
//        baseNavi.isPushing = NO;
//        return self.customPush;
//
//    }else if (operation == UINavigationControllerOperationPop){
//        return self.customPop;
//    }
//    return nil;
//}

- (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                            animationControllerForOperation:(UINavigationControllerOperation)operation
                                                         fromViewController:(UIViewController *)fromVC
                                                           toViewController:(UIViewController *)toVC {
    if (operation == UINavigationControllerOperationPush) {
        QABaseNavigationController *baseNavi = (QABaseNavigationController *)navigationController;
        baseNavi.isPushing = NO;
        if (![toVC isKindOfClass:[LYPictureBrowseViewController class]]) {
            return nil;
        }
        [baseNavi setNavigationBarHidden:YES animated:NO];
        
        return self.customPush;
        
    } else if (operation == UINavigationControllerOperationPop) {
        if (![fromVC isKindOfClass:[LYPictureBrowseViewController class]]) {
            return nil;
        }
        return self.customPop;
    }
    return nil;
}

- (nullable id <UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController
                                   interactionControllerForAnimationController:(nonnull id<UIViewControllerAnimatedTransitioning>)animationController{
    if (self.transitionParameter.gestureRecognizer)
        return self.percentIntractive;
    else
        return nil;
    
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    if([viewController isKindOfClass:[LYPictureBrowseViewController class]]){
        [navigationController setNavigationBarHidden:YES animated:YES];
    }else{
        
        //系统相册继承自 UINavigationController 这个不能隐藏 所有就直接return
        if ([navigationController isKindOfClass:[UIImagePickerController class]]) {
            return;
        }
        
        //不在本页时，显示真正的navbar
        [navigationController setNavigationBarHidden:NO animated:YES];
        //当不显示本页时，要么就push到下一页，要么就被pop了，那么就将delegate设置为nil，防止出现BAD ACCESS
        //之前将这段代码放在viewDidDisappear和dealloc中，这两种情况可能已经被pop了，self.navigationController为nil，这里采用手动持有navigationController的引用来解决
        if(navigationController.delegate == self){
            //如果delegate是自己才设置为nil，因为viewWillAppear调用的比此方法较早，其他controller如果设置了delegate就可能会被误伤
            navigationController.delegate = nil;
        }
    }
}

-(LYPictureBrowsePushAnimator *)customPush
{
    if (_customPush == nil) {
        _customPush = [[LYPictureBrowsePushAnimator alloc]init];
    }
    return _customPush;
}

- (LYPictureBrowsePopAnimator *)customPop {
    if (!_customPop) {
        _customPop = [[LYPictureBrowsePopAnimator alloc] init];
    }
    return _customPop;
}
- (LYPictureBrowsePercentDrivenInteractive *)percentIntractive{
    if (!_percentIntractive) {
        _percentIntractive = [[LYPictureBrowsePercentDrivenInteractive alloc] initWithTransitionParameter:self.transitionParameter];
    }
    return _percentIntractive;
}

-(void)setTransitionParameter:(LYPictureBrowseTransitionParameter *)transitionParameter{
    _transitionParameter = transitionParameter;
    
    self.customPush.transitionParameter = transitionParameter;
    self.customPop.transitionParameter = transitionParameter;
}

@end


